package com.test45_JUC;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * CountDownLatch：倒计数同步的线程管理机制
 */
public class Demo12_CountDownLatch {

    public static void main(String[] args) throws Exception {
        CountDownLatch latch = new CountDownLatch(3);   // 总共等待的数量
        for (int x = 0; x < 3; x++) {
            new Thread(() -> {
                System.out.printf("【%s】到达，并且已上车%n", Thread.currentThread().getName());
                latch.countDown();  // 等待数量减1
            }, "游客-" + x).start();
            TimeUnit.SECONDS.sleep(1);
        }
        latch.await();  // 等待计数为0后再解除阻塞
        System.out.println("【主线程】所有的旅客都到齐了，开车走人，去下一个景点购物消费。");
    }
}
